package com.tiger.cloud.service.crm;

import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.dao.crm.ActiveCountDao;
import com.tiger.cloud.dao.crm.UserActiveDetailDao;
import com.tiger.cloud.dao.crm.UserCompanyInfoDao;
import com.tiger.cloud.dao.crm.UserDao;
import com.tiger.cloud.enums.ActiveCountEnum;
import com.tiger.cloud.model.crm.ActiveCount;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.crm.UserActiveDetail;
import com.tiger.cloud.model.crm.UserCompanyInfo;
import com.tiger.cloud.model.ext.ActiveCountPojo;
import com.tiger.cloud.service.crm.IActiveCountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户日活跃数统计
 */
@Service("activeCountService")
@EnableScheduling
public class ActiveCountService extends TigerBaseService<ActiveCount> implements IActiveCountService {

    @Autowired
	private ActiveCountDao activeCountDao;

    @Autowired
	private UserDao userDao;

    @Autowired
	private UserCompanyInfoDao userCompanyInfoDao;

    @Autowired
	private UserActiveDetailDao userActiveDetailDao;

	@Override
	public MybatisTigerDao<ActiveCount> getEntityDao() {
		return activeCountDao;
	}

	@Override
	public boolean insertActiveCount(ActiveCountPojo activeCountPojo) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(activeCountPojo == null){
			return false;
		}
		User user = userDao.findOneByUserId(activeCountPojo.getId());
		if(user == null){
			return  false;
		}
		UserCompanyInfo userCompanyInfo = new UserCompanyInfo();
		userCompanyInfo.setUserId(activeCountPojo.getId());
		List<UserCompanyInfo> resultCompany = userCompanyInfoDao.findByEntity(userCompanyInfo);
        if(resultCompany.isEmpty()){
          return false;
		}
		userCompanyInfo = resultCompany.get(0);
		ActiveCount activeCount = new ActiveCount();
		activeCount.setActiveTime(new Date());
		activeCount = activeCountDao.findByActiveTime(activeCount);
		if(null == user.getActiveTime()){
			user.setActiveTime(new Date());
			if(userDao.update(user) > 0){
				return insertOrUpdateActiveCount(activeCount, activeCountPojo,userCompanyInfo);
			}else{
				return false;
			}
		}
		if(sdf.format(user.getActiveTime()).equals(sdf.format(new Date()))){
			return true;
		}else{
			user.setActiveTime(new Date());
			if(userDao.update(user) > 0){
				return insertOrUpdateActiveCount(activeCount,activeCountPojo,userCompanyInfo);
			}else{
				return false;
			}
		}
	}
    private boolean insertOrUpdateActiveCount(ActiveCount activeCount, ActiveCountPojo activeCountPojo,UserCompanyInfo userCompanyInfo){
		Integer activeNum = 1;
		Integer androidCount = 1;
		Integer iosCount = 1;
		Integer pageCount =1;
		UserActiveDetail userActiveDetail = new UserActiveDetail();
		userActiveDetail.setUserId(activeCountPojo.getId());
		userActiveDetail.setActiveTime(new Date());
		List<UserActiveDetail> resultList= userActiveDetailDao.findByEntity(userActiveDetail);
		if(resultList.isEmpty()){
			userActiveDetail = new UserActiveDetail();
			userActiveDetail.setUserId(activeCountPojo.getId());
			userActiveDetail.setActiveTime(new Date());

			userActiveDetail.setClientType(activeCountPojo.getActiveType());

			userActiveDetail.setProvinceId(userCompanyInfo.getProvinceId());
			userActiveDetail.setProvinceName(StringUtils.isEmpty(userCompanyInfo.getProvinceName()) == true ? "":userCompanyInfo.getProvinceName());
			userActiveDetail.setCityId(userCompanyInfo.getCityId());
			userActiveDetail.setCityName(StringUtils.isEmpty(userCompanyInfo.getCityName()) == true ? "" : userCompanyInfo.getCityName());
			userActiveDetail.setDistrictId(userCompanyInfo.getDistrictId());
			userActiveDetail.setDistrictName(StringUtils.isEmpty(userCompanyInfo.getDistrictName()) == true ? "" : userCompanyInfo.getDistrictName());

			userActiveDetailDao.insert(userActiveDetail);
		}
		if(activeCount == null){
			activeCount = new ActiveCount();
			activeCount.setActiveCount(activeNum);
			activeCount.setActiveTime(new Date());
			if(activeCountPojo.getActiveType() == ActiveCountEnum.ANDROID_COUNT){
				activeCount.setAndroidActiveCount(androidCount);
			}else if(activeCountPojo.getActiveType() == ActiveCountEnum.IOS_COUNT){
				activeCount.setIosActiveCount(iosCount);
			}else{
				activeCount.setPageActiveCount(pageCount);
			}
			return activeCountDao.insert(activeCount) > 0;
		}else{
			activeCount.setActiveCount(activeCount.getActiveCount()+activeNum);
			if(activeCountPojo.getActiveType() == ActiveCountEnum.ANDROID_COUNT){
				activeCount.setAndroidActiveCount(activeCount.getAndroidActiveCount()+androidCount);
			}else if(activeCountPojo.getActiveType() == ActiveCountEnum.IOS_COUNT){
				activeCount.setIosActiveCount(activeCount.getIosActiveCount()+iosCount);
			}else{
				activeCount.setPageActiveCount(activeCount.getPageActiveCount()+pageCount);
			}
			return activeCountDao.update(activeCount) > 0;
		}
	}
	@Scheduled(cron="0 0 2 * * ? ")
	public void scheduleJob() throws Exception {
		try {
			Date date=new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			date = calendar.getTime();
			ActiveCount activeCount = new ActiveCount();
			activeCount.setActiveTime(date);
			activeCount = activeCountDao.findByActiveTime(activeCount);

			Integer userCount = userDao.userCount();
			if(activeCount == null){
				activeCount = new ActiveCount();
				activeCount.setUserCount(userCount);
				activeCount.setActiveTime(date);
				activeCountDao.insert(activeCount);
				return;
			}else{
				long activeCountId = activeCount.getId();
				activeCount = new ActiveCount();
				activeCount.setId(activeCountId);
				activeCount.setUserCount(userCount);
				activeCountDao.update(activeCount);
				System.out.println("定时任务======>修改用户统计人数");
				return;
			}


		} catch (Exception e){
			System.out.println("定时任务出错======>"+e.getMessage().toLowerCase());
		}
		throw new Exception("======>修改用户活跃统计总人数失败!");
	}
}